package com.eliota.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eliota.common.R;
import com.eliota.entity.Device;
import com.eliota.entity.Maintain;
import com.eliota.service.DeviceService;
import com.eliota.service.MaintainService;
import com.eliota.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MG
 * @since 2023-04-02
 */
@RestController
@RequestMapping("/maintain")
public class MaintainController {

    @Resource
    private MaintainService maintainService;

    @Resource
    private DeviceService deviceService;

    @Resource
    private UserService userService;


    /**
     * 插入
     * @param maintain
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Maintain maintain){
        return R.success(maintainService.saveOrUpdate(maintain));
    }


    /**
     *查询条件：deviceName：维修设备名
     *
     *
     * @param pageNum
     * @param pageSize
     * @param deviceName 维修设备名
     *
     * mt_level          排序规则：升序
     * @return
     */
    @GetMapping("/page")
    public R findPage(@RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam String deviceName){
        Page<Maintain> maintainPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Maintain> maintainQueryWrapper= new QueryWrapper<>();
        maintainQueryWrapper.like("device_name",deviceName).orderByAsc("mt_level");

        return R.success(maintainService.page(maintainPage,maintainQueryWrapper));
    }


    @GetMapping("/{mtId}")
    public R findByUserName(@PathVariable Integer mtId){
        Maintain getOneById = maintainService.getOne(new QueryWrapper<Maintain>().eq("mt_id", mtId));
        return R.success(getOneById);
    }



    @DeleteMapping("/{mtId}")
    public R delete(@PathVariable Integer mtId){
        maintainService.statusChange(mtId);
        return R.success();
    }

    @PostMapping("/bath/del")
    public R deleteBath(@RequestBody List<Integer> ids){
        maintainService.deleteBatchByIds(ids);
        return R.success();
    }

    @GetMapping("/device/list")
    public R findDevices(){
        return R.success(deviceService.list());
    }

    @GetMapping("/user/list")
    public R findUsers(){
        return R.success(userService.list());
    }



}
